﻿using MMK.Paket_Tracker.Runtime;
using System;
using MMK.Paket_Tracker.Runtime.ValidationRules;

namespace MMK.Paket_Tracker.GUI.Models
{
    /// <summary>
    /// Model for receiver data
    /// </summary>
    public class ReceiverModel : ModelBase
    {
        /// <summary>
        /// Gets or sets the first name.
        /// </summary>
        /// <value>The first name.</value>
        public String FirstName
        {
            get
            {
                return vorname;
            }
            set
            {
                if (vorname == value) return;
                vorname = value;
                RaisePropertyChanged(Fields.FIRST_NAME);
            }
        }
        /// <summary>
        /// Gets or sets the last name.
        /// </summary>
        /// <value>The last name.</value>
        public String LastName
        {
            get
            {
                return nachname;
            }
            set
            {
                if (nachname == value) return;
                nachname = value;
                RaisePropertyChanged(Fields.LAST_NAME);
            }
        }
        /// <summary>
        /// Gets or sets the street.
        /// </summary>
        /// <value>The street.</value>
        public String Street
        {
            get
            {
                return strasse;
            }
            set
            {
                if (strasse == value) return;
                strasse = value;
                RaisePropertyChanged(Fields.STREET);
            }
        }
        /// <summary>
        /// Gets or sets the number.
        /// </summary>
        /// <value>The number.</value>
        public String Number
        {
            get
            {
                return hausnummer;
            }
            set
            {
                if (hausnummer == value) return;
                hausnummer = value;
                RaisePropertyChanged(Fields.NUMBER);
            }
        }
        /// <summary>
        /// Gets or sets the post code.
        /// </summary>
        /// <value>The post code.</value>
        public int PostCode
        {
            get
            {
                return plz;
            }
            set
            {
                if (plz == value) return;
                plz = value;
                RaisePropertyChanged(Fields.POST_CODE);
            }
        }
        /// <summary>
        /// Gets or sets the town.
        /// </summary>
        /// <value>The town.</value>
        public String Town
        {
            get
            {
                return ort;
            }
            set
            {
                if (ort == value) return;
                ort = value;
                RaisePropertyChanged(Fields.TOWN);
            }
        }


        /// <summary>
        /// Gets or sets a value indicating whether this instance has an error.
        /// </summary>
        /// <value><c>true</c> if this instance has an error; otherwise, <c>false</c>.</value>
        public Boolean HasError
        {
            get { return hasError; }
            set { hasError = value; }
        }


        /// <summary>
        /// Gets the <see cref="System.String"/> with the specified column name.
        /// </summary>
        /// <value></value>
        public override string this[string columnName]
        {

            get
            {
                String result = null;
                String columnResource = null;

                switch (columnName)
                {
                    case Fields.FIRST_NAME:
                        result = AdressValidator.Validate(columnName, FirstName);
                        break;
                    case Fields.LAST_NAME:
                        result = AdressValidator.Validate(columnName, LastName);
                        break;
                    case Fields.TOWN:
                        result = AdressValidator.Validate(columnName, Town);
                        break;
                    case Fields.STREET:
                        result = AdressValidator.Validate(columnName, Street);
                        break;
                    case Fields.NUMBER:
                        result = AdressValidator.Validate(columnName, Number);
                        break;
                    case Fields.POST_CODE:
                        result = AdressValidator.Validate(columnName, PostCode);
                        break;
                    default:
                        break;
                }

                columnResource = ResourceStrings.ResourceManager.GetString(columnName);

                if (result == null)
                {
                    if (ErrorDictionary.ContainsKey(columnResource))
                    {
                        ErrorDictionary.Remove(columnResource);
                    }
                }
                else
                {
                    if (ErrorDictionary.ContainsKey(columnResource))
                    {
                        ErrorDictionary.Remove(columnResource);
                    }
                    ErrorDictionary.Add(columnResource, result);
                }

                hasError = (ErrorDictionary.Count > 0);

                return result;
            }
        }


        private String vorname;
        private String nachname;
        private String ort;
        private String strasse;
        private int plz;
        private String hausnummer;
        private Boolean hasError;

    }

}
